package cn.piaoyi.web.action;

import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.Category;
import cn.piaoyi.bean.Poster;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.CategoryService;
import cn.piaoyi.service.PosterService;
import cn.piaoyi.web.servlet.AdminSession;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class AdminPoster extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminPoster.class);
	
	private PosterService posterService;

	public PosterService getPosterService() {
		return posterService;
	}

	public void setPosterService(PosterService posterService) {
		this.posterService = posterService;
	}



	private long id;
	private String name;
	private String image;
	private String direct;
	private int orders;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getDirect() {
		return direct;
	}

	public void setDirect(String direct) {
		this.direct = direct;
	}

	public int getOrders() {
		return orders;
	}

	public void setOrders(int orders) {
		this.orders = orders;
	}
	
	private List posterList;

	public List getPosterList() {
		return posterList;
	}

	public void setPosterList(List posterList) {
		this.posterList = posterList;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		List l = this.getPosterService().findPosters("orders", 0);
		this.setPosterList(l);
		return SUCCESS;
	}
	
	public String add(){
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		//添加海报
		Poster p = new Poster();
		p.setName(this.name);
		p.setImage(this.image);
		p.setDirect(this.direct);
		p.setOrders(this.orders);
		try {
			this.getPosterService().createPoster(p);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		this.setAction("doupdate");
		Poster p = this.getPosterService().findPosterById(this.id);
		this.setName(p.getName());
		this.setImage(p.getImage());
		this.setDirect(p.getDirect());
		this.setOrders(p.getOrders());
		return INPUT;
	}
	
	public String doupdate(){

		Poster p = this.getPosterService().findPosterById(this.id);
		p.setName(this.name);
		p.setImage(this.image);
		p.setDirect(this.direct);
		p.setOrders(this.orders);
		try {
			this.getPosterService().updatePoster(p);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		this.getPosterService().removePosterById(this.id);
		return "index";
	}
}
